package com.csun.cmny.provider.controller;


import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.provider.model.domain.OmcFirstLvelMerchantBind;
import com.csun.cmny.provider.model.domain.OmcMerchant;
import com.csun.cmny.provider.model.dto.merchant.MerchantQueryDto;
import com.csun.cmny.provider.model.dto.merchant.MerchantSaveDto;
import com.csun.cmny.provider.model.vo.Merchant.MerchantVo;
import com.csun.cmny.provider.pojo.User;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcFirstLvelMerchantBindService;
import com.csun.cmny.provider.service.OmcMerchantService;
import com.csun.cmny.provider.service.UacRpcService;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping(value = "/merchant", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcMerchantController {

    @Resource
    private OmcMerchantService merchantService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcFirstLvelMerchantBindService firstLvelMerchantBindService;
    @Resource
    private UacRpcService uacRpcService;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;


    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "商户新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addOmcContract(@RequestBody @Valid MerchantSaveDto merchantSaveDto, BindingResult br) throws ParseException {



        OmcMerchant omcMerchant = new OmcMerchant();

        BeanUtils.copyProperties(merchantSaveDto,omcMerchant);


        int save = merchantService.save(omcMerchant);



        for (Long firstLevelId : merchantSaveDto.getFirstLevelIds()) {

            OmcFirstLvelMerchantBind omcFirstLvelMerchantBind = new OmcFirstLvelMerchantBind();

            omcFirstLvelMerchantBind.setFirstLevelId(firstLevelId);
            omcFirstLvelMerchantBind.setMerchantId(omcMerchant.getId());

            firstLvelMerchantBindService.save(omcFirstLvelMerchantBind);
        }


        User user = new User();





        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),save);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "商户分页查询")
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public Wrapper select(@RequestBody @Valid MerchantQueryDto merchantQueryDto){


        List<MerchantVo> list=merchantService.selectMerchant(merchantQueryDto);
        PageInfo<MerchantVo> pageInfo = new PageInfo<>(list);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }
}
